#pragma once

#include <mutex>
#include <string>
#include <unordered_map>
#include "ShmQueue.hpp"



class ShmQueueManager {
    enum FunctionType {
        FUNC_NULL = 0,
        PUSH = 1,
        POP = 2,
        FRONT = 3
    };

public:
    // 获取单例实例的静态方法
    static ShmQueueManager* GetInstance() {
        std::lock_guard<std::mutex> lock(_mutex);
        if (_instance == nullptr) {
            _instance = new ShmQueueManager();
        }
        return _instance;
    }

    // 销毁单例实例的静态方法
    static void DestroyInstance() {
        std::lock_guard<std::mutex> lock(_mutex);
        if (_instance != nullptr) {
            delete _instance;
            _instance = nullptr;
        }
    }
public:

    void Execute(ShmMessageNode*& mesNode) {
        DataStructureBlock block = mesNode->GetDataStructureBlock();
        int flag = -1;
        switch (block._functionType) {
            case PUSH:
                LOG_CYC(NORMAL, "执行Queue::Push, Queue：%s", block.buffer);
                flag = Push(mesNode);
                break;

            case POP:
                LOG_CYC(NORMAL, "执行Queue::Pop, Queue：%s", block.buffer);
                flag = Pop(mesNode);
                break;
 
            case FRONT:
                LOG_CYC(NORMAL, "执行Queue::front, Queue：%s", block.buffer);
                flag = front(mesNode);
                break;

            default:
                LOG_CYC(WARNING, "Queue:未定义功能", nullptr);
                break;
        }

        if (flag == 0)
            mesNode->SetExecuteSuccess(true);
        else
            mesNode->SetExecuteSuccess(false);
    }

private:

    int Push(ShmMessageNode*& mesNode) {
        const std::string& name = mesNode->GetDataStructureBlock().buffer;
        ShmSharedPtrNode* node = reinterpret_cast<ShmSharedPtrNode*>(shmServiceManger->GetShmPtr(mesNode->GetShmMemoryBlock()));

        std::lock_guard<std::mutex> lock(_mapMutex);

        ShmQueue* que = GetQueue(name);
        que->push(node);

        return 0;
    }

    int Pop(ShmMessageNode*& mesNode) {
        const std::string& name = mesNode->GetDataStructureBlock().buffer;

        std::lock_guard<std::mutex> lock(_mapMutex);

        ShmQueue* que = GetQueue(name);
        que->pop();
        if (que->empty()) {
            delete que;
            _queueNames.erase(name);
        }

        return 0;
    }

    int front(ShmMessageNode*& mesNode) {
        const std::string& name = mesNode->GetDataStructureBlock().buffer;

        std::lock_guard<std::mutex> lock(_mapMutex);

        ShmQueue* que = GetQueue(name);
        ShmSharedPtrNode* node = que->front();
        mesNode->SetShmMemoryBlock(node->myblock._shmID, node->myblock._shmPos, node->myblock._blockSize);

        return 0;

    }

    // 获取指定队列
    ShmQueue* GetQueue(const std::string& name) {

        auto it = _queueNames.find(name);
        if (it != _queueNames.end())
            return _queueNames[name];

        _queueNames[name] = new ShmQueue;
        return _queueNames[name];
    }

private:
    // 私有构造函数，防止外部实例化
    ShmQueueManager() {
        shmServiceManger = ShmServiceManger::GetInstance();
    }

    // 禁用拷贝构造函数和赋值运算符
    ShmQueueManager(const ShmQueueManager&) = delete;
    ShmQueueManager& operator=(const ShmQueueManager&) = delete;

    // 禁用移动构造函数和赋值运算符
    ShmQueueManager(ShmQueueManager&&) = delete;
    ShmQueueManager& operator=(ShmQueueManager&&) = delete;

    // 静态成员变量，用于存储单例实例
    static ShmQueueManager* _instance;
    static std::mutex _mutex;

private:

    std::unordered_map<std::string, ShmQueue*> _queueNames;
    ShmServiceManger* shmServiceManger = nullptr;
    mutable std::mutex _mapMutex;            // 用于保护unordered_map的互斥锁
};

// 静态成员变量的定义和初始化
ShmQueueManager* ShmQueueManager::_instance = nullptr;
std::mutex ShmQueueManager::_mutex;
